package com.cheer.ba.presenter.impl;

import android.content.Context;

import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.ReturnCode;
import com.cheer.ba.http.BaseCallback;
import com.cheer.ba.http.CallManager;
import com.cheer.ba.http.HandleRetCode;
import com.cheer.ba.http.Http;
import com.cheer.ba.http.request.APIService;
import com.cheer.ba.model.MyAddressModel;
import com.cheer.ba.model.ResponseData;
import com.cheer.ba.presenter.GeneralPresenter;
import com.cheer.ba.utils.NetUtils;
import com.cheer.ba.view.base.IViewController;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 登录处理器
 *
 * @author HQ
 *         created at 2017/3/3 0003 15:50
 */
public class MyAddressPresenter extends GeneralPresenter {

    private Context mContext;
    private IViewController mView;
    private APIService mService;

    @Override
    public void attachView(IViewController view) {
        this.mView = view;
        mContext = getContext(view);
        mService = Http.getUserService(mContext);
    }

    /**
     * 添加常用地址
     */
    public void addMyAddress(String name, String tel, String location) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        Call<ResponseData<MyAddressModel>> call = mService.addMyAddress(1003, Session.getUserId(), name, tel, location);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<MyAddressModel>>() {
            @Override
            public void onResponse(Call<ResponseData<MyAddressModel>> call, Response<ResponseData<MyAddressModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<MyAddressModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        MyAddressModel addressModel = res.getData();
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "添加成功");
                        mView.updateViewWithTag(addressModel, ConstantTag.ADD_SUCCESS);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "添加失败");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "添加失败");
            }

            @Override
            public void onFailure(Call<ResponseData<MyAddressModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "添加失败");
            }
        });
    }

    /**
     * 移除常用地址
     */
    public void delMyAddress(String addressId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        Call<ResponseData<MyAddressModel>> call = mService.delMyAddress(1003, Session.getUserId(), addressId);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<MyAddressModel>>() {
            @Override
            public void onResponse(Call<ResponseData<MyAddressModel>> call, Response<ResponseData<MyAddressModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<MyAddressModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        MyAddressModel addressModel = res.getData();
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag(addressModel, ConstantTag.DEL_SUCCESS);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "移除失败");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "移除失败");
            }

            @Override
            public void onFailure(Call<ResponseData<MyAddressModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "移除失败");
            }
        });
    }

    /**
     * 获得我的默认地址
     */
    public void getMyDefaultAddress(boolean isShowLoad) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET);
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING);
        }
        Call<ResponseData<MyAddressModel>> call = mService.getMyDefaultAddress(1003, Session.getUserId());
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<MyAddressModel>>() {
            @Override
            public void onResponse(Call<ResponseData<MyAddressModel>> call, Response<ResponseData<MyAddressModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<MyAddressModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        MyAddressModel addressModel = res.getData();
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL);
                        mView.updateViewWithTag(addressModel, ConstantTag.GET_MY_DEFAULT_ADDRESS);
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }

            @Override
            public void onFailure(Call<ResponseData<MyAddressModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }
        });
    }

    /**
     * 默认地址
     */
    public void defaultMyAddress(String addressId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        Call<ResponseData<MyAddressModel>> call = mService.defaultMyAddress(1003, Session.getUserId(), addressId);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<MyAddressModel>>() {
            @Override
            public void onResponse(Call<ResponseData<MyAddressModel>> call, Response<ResponseData<MyAddressModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<MyAddressModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        MyAddressModel addressModel = res.getData();
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag(addressModel, ConstantTag.DEFAULT_SUCCESS);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "设置默认地址失败");
                        mView.updateViewWithTag("",ConstantTag.FAIL_ACTION);
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "设置默认地址失败");
                mView.updateViewWithTag("", ConstantTag.FAIL_ACTION);
            }

            @Override
            public void onFailure(Call<ResponseData<MyAddressModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "设置默认地址失败");
                mView.updateViewWithTag("", ConstantTag.FAIL_ACTION);
            }
        });
    }

    /**
     * 获得我的地址列表
     */
    public void getMyAddressList(boolean isShowLoad) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET);
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING);
        }
        Call<ResponseData<List<MyAddressModel>>> call = mService.getMyAddress(1003, Session.getUserId());
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<MyAddressModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<MyAddressModel>>> call, Response<ResponseData<List<MyAddressModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<MyAddressModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<MyAddressModel> addressModel = res.getData();
                        if (null != addressModel) {
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL);
                            mView.updateViewWithTag(addressModel, ConstantTag.GET_LIST_SUCCESS);
                            return;
                        }
                    }
                }
                mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }

            @Override
            public void onFailure(Call<ResponseData<List<MyAddressModel>>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }
        });
    }


    @Override
    public void detachView() {
    }

    @Override
    public Object getCacheData() {
        return null;
    }
}
